<?php
/**
 * 首页
 * User: tsang
 * Date: 2017/9/14
 * Time: 20:11
 */
namespace app\admin\controller;
use Doctrine\Common\Cache\MemcacheCache;
use think\Session;
use think\cache\driver\Redis;
class Activity extends Common
{
    /*
    *构造函数
    */
    function __construct(){
        parent::__construct();
    }
    /**
    *活动列表
    */
    public function index(){      
        $list = db('activity')->where(['status'=>'1'])->paginate(10)->each(function($key){
            $key['start_time'] = date('Y/m/d',$key['start_time']);
            $key['end_time'] = date('Y/m/d',$key['end_time']);

            return $key;
        });
        if(request()->isAjax()) {
            $id = input('id');
            $status = input('status');
            $bool = db('activity')->where(['id'=>$id])->update(['status'=>$status]);
            if(!$bool)$this->error('操作失败');
            $this->success('操作成功');
        }
        $url = "http://".$_SERVER['SERVER_NAME']."/Index/index.html";
        $this->assign('url',$url);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
    *添加活动
    */
    public function add(){
        $activity_id = input('activity_id');
      //  var_dump($activity_id);exit();
        if (!empty($activity_id)){
            ///echo 1111;exit();
            $activity = db('activity')->where(['id'=>$activity_id])->find();
            //var_dump($activity);exit();
            $activity['start_time'] = date('Y-m-d',$activity['start_time']);
            $activity['end_time'] = date('Y-m-d',$activity['end_time']);
        }
        //echo 222222;exit();
        if(request()->isAjax()) {
            //echo 000;exit();
            $title = input('title');
            $start_time = input('start_time');
            $start_time = strtotime($start_time);
            $end_time = input('end_time');
            $end_time = strtotime($end_time);
            $lottery_time = input('lottery_time');
            $number_draws = input('number_draws');
            $description = input('content');

            $data = [
                'title'=>$title,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'lottery_time'=>$lottery_time,
                'number_draws'=>$number_draws,
                'description'=>$description,
                'time'=>time()
            ];

            if(empty($activity_id)){
                db('activity')->insert($data);
            }else{
                db('activity')->where(['id'=>$activity_id])->update($data);
                $this->assign('activity',$activity);
                $this->assign('activity_id',$activity_id);
                $this->success('修改成功',url('Activity/index'));
            }
            $this->success('添加成功',url('Activity/index'));
        }
        return $this->fetch();
    }

    public function edit(){
        $activity_id = input('activity_id');
        $activity = db('activity')->where(['id'=>$activity_id])->find();

        $activity['start_time'] = date('Y-m-d h:i:s',$activity['start_time']);
        $activity['end_time'] = date('Y-m-d h:i:s',$activity['end_time']);

        $this->assign('activity',$activity);
        $this->assign('activity_id',$activity_id);
        return $this->fetch();

    }
    public function doedit(){

         $activity_id = input('activity_id');
           // var_dump($activity_id);exit();
            $title = input('title');
            $start_time = input('start_time');
            $start_time = strtotime($start_time);
            $end_time = input('end_time');
            $end_time = strtotime($end_time);
            $lottery_time = input('lottery_time');
            $number_draws = input('number_draws');
            $description = input('content');
        

            $data = [
                'title'=>$title,
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'lottery_time'=>$lottery_time,
                'number_draws'=>$number_draws,
                'description'=>$description,
                'time'=>time()
               
            ];
          
             $res=db('activity')->where(['id'=>$activity_id])->update($data);

             if ($res) {
                     $this->success('修改成功',url('Activity/index'));
                }   
                $this->success('修改失败',url('Activity/index'));
    }
    /**
    * 抽奖人
    */
    public function lotteryer(){
        $activity_id = input('activity_id');

        $list = db('activityer')->where(['aid'=>$activity_id])->paginate(10);

        if(request()->isAjax()) {
            $ids = input('ids/a');
            $ids = explode(',', $ids[0]);
            
            db('activityer')->where(['aid'=>$activity_id,'id'=>['in',$ids]])->delete();

            $this->success('删除成功',url('Activity/lotteryer',['activity_id'=>$activity_id]));
        }

        $this->assign('activity_id',$activity_id);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
    *   抽奖人添加
    */
    public function lotteryer_add(){
        $activity_id = input('activity_id');
        $mobile = input('mobile/a');
         
        if(request()->isAjax()) {
            $mobile_array = explode(',', $mobile[0]);
           
            $where['aid'] = $activity_id;
            $where['mobile'] = ['in',$mobile_array];

            $activityer = db('activityer')->where($where)->select();

            if(!empty($activityer)){
                $mobile_string = array_column($activityer, 'mobile');
                $error_mobile = implode(",", $mobile_string);
                $this->error("以上电话\"{$error_mobile}\"已入库,请不要重复入库!");
            }

            $mobile_array_count = count($mobile_array);

            for($i=0;$i<$mobile_array_count;$i++){
                $data[]  = [
                    'aid' => $activity_id,                   //活动id
                    'mobile' => $mobile_array[$i],           // 电话
                ];
            }

            db('activityer')->insertAll($data);

            $this->success('添加成功',url('Activity/lotteryer',['activity_id'=>$activity_id]));
        }

        $this->assign('activity_id',$activity_id);
        return $this->fetch();
    }
}